﻿/***************************************************/
/*** file:IMLogic.cs                             ***/
/*** date:2010-08-07                             ***/
/*** author:huas                                 ***/
/*** function:implement Logic of IM              ***/
/***************************************************/


using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using IM.Common;

namespace IM.Logic
{
    class IMLogic : IHanleServer, IHanleSink
    {
        private IHanleServer m_logicServer = null;
        private IHanleSink m_handleSink = null;

        private IMLogicHandle m_logicHandle;
        private NetHeader m_netHeader;

        public IMLogic(IHanleSink handleSink)
        {
            m_handleSink = handleSink;
            m_netHeader = new NetHeader();
            m_logicHandle = new IMLogicHandle(this);
            m_logicHandle.CreateThread(0);
        }

        public int OnReceiveFromClient(byte[] bData, int nBeginIndex, UInt32 nDataLen)
        {
            return m_logicHandle.OnReceiveFromClient(bData, nBeginIndex, nDataLen);
        }

        public UInt32 GetPackageLen(byte[] szData, int nOffset, uint nLen)
        {
            UInt32 uPackageLen = BitConverter.ToUInt32(szData, nOffset);
            if (nLen <= 4 || nLen < uPackageLen)
            {
                return 0;
            }
            return uPackageLen;
        }

        public void SetHandleSink(IHanleSink iSink)
        {
            m_handleSink = iSink;
        }

        public int OnSendToClient(byte[] bData, int nDataLen, ref NetHeader header)
        {
            if (m_handleSink == null)
            {
                return -1;
            }
            return m_handleSink.OnSendToClient(bData, nDataLen, ref header);
        } 
    }

}
